﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UserDefineForm.formcomponent;
using System.Windows.Forms;
using UserDefineForm.dao.orm;
using UserDefineForm.control;
using System.Drawing;
using System.IO;

namespace UserDefineForm.dao.supplierform
{
    public class SupplierFormService
    {
        SupplierFormGUI gui;

        private ControlDAO controlDAO;
        private SupplierFormDAO formDAO;
        private ListItemDAO listItemDAO;


        public SupplierFormService(SupplierFormGUI gui)
        {
            this.gui = gui;

            controlDAO = SystemFactory.getControlDAO();
            formDAO = SystemFactory.getFormDAO();
            listItemDAO = SystemFactory.getListItemDAO();
        }

        public void OpenForm(int formId)
        {
            SupplierFormORM formOrm = formDAO.getFormById(formId);
            if (formOrm == null) return;

            gui.reset();

            List<ControlORM> listControlORM = controlDAO.getAllControlsByFormID(formId);

            List<DesignableControlBase> listControl = new List<DesignableControlBase>();

            foreach( ControlORM orm in listControlORM)
            {
                DesignableControlBase control = orm.toControl(gui);
                listControl.Add(control);
                if (orm.Type == (int)ControlType.ComboBox)
                {
                    List<ListItemORM> listItemOrm = listItemDAO.getAllListItemByControlId(orm.Id);
                    List<string> listItemValue = new List<string>();

                    foreach (ListItemORM itemOrm in listItemOrm)
                    {
                        listItemValue.Add(itemOrm.Value);
                    }
                    control.ListValue = listItemValue;
                }

            }

            gui.nameTxt.Text = formOrm.Name;
            gui.designPanel.setBackGround(byteArrayToImage(formOrm.Background));

            foreach (DesignableControlBase control in listControl)
            {
                gui.designPanel.addDesignableControl(control);
            }

            gui.Refresh();


        }


        public void SaveCurrentForm()
        {
            SupplierFormORM formORM = SystemFactory.getCurrentForm();
            
            if (formORM.Id  <0 )
            {
                SaveNewForm();
            }
            else
            {
                UpdateCurrentForm();
            }

        }
        private void SaveNewForm()
        {
            SupplierFormORM formORM = SystemFactory.getCurrentForm();

            string formName = gui.nameTxt.Text.Trim();
            if (formName == null || formName.Length == 0)
            {
                MessageBox.Show("Form's name is empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (checkExistedName(formName))
            {
                MessageBox.Show("Existed form name, try another one", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            formORM.Name = formName;
            if (gui.designPanel.getBackground() != null)
            {
                formORM.Background = imageToByteArray(gui.designPanel.getBackground());
            }
            formDAO.insertForm(formORM);

            System.Windows.Forms.Control.ControlCollection controlColl = gui.designPanel.getAllDesignableControl();

            foreach (Control ctrl in controlColl)
            {
                DesignableControlBase designableCtrl = null;
                if (ctrl is DesignableControlBase)
                {
                    designableCtrl = (DesignableControlBase)ctrl;
                }
                else
                {
                    continue;   
                }

                designableCtrl.FormId = formORM.Id;

                ControlORM controlORM = new ControlORM(designableCtrl);

                // add control to DB
                controlDAO.insertControl(controlORM);
                //

                if (designableCtrl is DesignableComboBox)
                {
                    List<string> listItemValue = designableCtrl.ListValue;
                    foreach (string itemStr in listItemValue)
                    {
                        ListItemORM itemOrm = new ListItemORM();
                        itemOrm.ControlId = controlORM.Id;
                        itemOrm.Value = itemStr;

                        // add list item to DB
                        listItemDAO.insertListItem(itemOrm);

                    }
                }

            }


        }

        public void DeleteAllControlsOfForm(int formId)
        {
            List<ControlORM> listContrlOrm = controlDAO.getAllControlsByFormID(formId);
            foreach (ControlORM controlOrm in listContrlOrm)
            {
                listItemDAO.deleteAllListItemByControlId(controlOrm.Id);
                controlDAO.deleteControl(controlOrm.Id);
            }
        }

        

        private void UpdateCurrentForm()
        {
            SupplierFormORM formORM = SystemFactory.getCurrentForm();
            DeleteForm(formORM.Id);
            SaveNewForm();
        }

        public void DeleteForm(int formId)
        {
            DeleteAllControlsOfForm(formId);
            formDAO.deleteForm(formId);
        }

        

        private bool checkExistedName(string name)
        {
            List<string> listName ;
            formDAO.getAllFormIdAndName(out listName);
            if (listName.Contains(name))
            {
                return true;
            }
            return false;
        }

        private byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            if (imageIn == null) return null;
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }

        private Image byteArrayToImage(byte[] byteArrayIn)
        {
            if (byteArrayIn == null) return null;
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
    }
}
